package com.binance.notification.web.service.abnormaltrade.impl;

import java.util.List;

import com.binance.notification.api.push.notification.AbstractNotification;
import com.binance.notification.api.push.notification.AndroidPayload;
import com.binance.notification.api.push.notification.IosPayload;
import com.binance.notification.api.push.notification.PcPayload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.binance.notification.api.push.notification.MultipleNotificationPacket;
import com.binance.notification.api.push.notification.WebPayload;
import com.binance.notification.web.service.IPushService;
import com.binance.notification.web.service.abnormaltrade.IAbnormalTradePushNoticeService;
import com.binance.notification.web.service.abnormaltrade.enums.AbnormalTradePushNoticeBusTypeEnum;
import com.binance.notification.web.service.abnormaltrade.log.LoggerUtils;
import com.google.common.collect.Lists;

@Service
public class AbnormalTradePushNoticeServiceImpl implements IAbnormalTradePushNoticeService {
    private static final org.slf4j.Logger log = LoggerUtils.getLogger(AbnormalTradePushNoticeServiceImpl.class);
    @Autowired
    private IPushService pushService;
    @Value("${jpush.apc.web.enabled:true}")
    private boolean webApcEnabled;
    @Value("${jpush.apc.pc.enabled:false}")
    private boolean pcApcEnabled;
    @Value("${jpush.apc.android.enabled:false}")
    private boolean androidApcEnabled;
    @Value("${jpush.apc.ios.enabled:false}")
    private boolean iosApcEnabled;

    @Value("${jpush.pb.web.enabled:true}")
    private boolean webPbEnabled;
    @Value("${jpush.pb.pc.enabled:false}")
    private boolean pcPbEnabled;
    @Value("${jpush.pb.android.enabled:false}")
    private boolean androidPbEnabled;
    @Value("${jpush.pb.ios.enabled:false}")
    private boolean iosPbEnabled;

    @Override
    public void pushWebMessage(AbnormalTradePushNoticeBusTypeEnum abnormalTradePushNoticeBusTypeEnum, String title,
            String content) {
        log.info("AbnormalTradePushNoticeServiceImpl.pushWebMessage ......，the content is : {}", content);
        String busType = abnormalTradePushNoticeBusTypeEnum.name();
        List<MultipleNotificationPacket> packets = Lists.newArrayList();
        MultipleNotificationPacket packet = new MultipleNotificationPacket();
        if ("APC".equals(busType)) {
            // 价格异动
            if (this.webApcEnabled) {
                packet.setWebMessage((WebPayload) this.createPayload("web", title, content, busType));
            }
            if (this.androidApcEnabled) {
                packet.setAndroidMessage((AndroidPayload) this.createPayload("android", title, content, busType));
            }
            if (this.pcApcEnabled) {
                packet.setPcMessage((PcPayload) this.createPayload("windows", title, content, busType));
            }
            if (this.iosApcEnabled) {
                packet.setIosMessage((IosPayload) this.createPayload("ios", title, content, busType));
            }
        } else if ("PB".equals(busType)) {
            // 价格突破
            if (this.webPbEnabled) {
                packet.setWebMessage((WebPayload) this.createPayload("web", title, content, busType));
            }
            if (this.androidPbEnabled) {
                packet.setAndroidMessage((AndroidPayload) this.createPayload("android", title, content, busType));
            }
            if (this.pcPbEnabled) {
                packet.setPcMessage((PcPayload) this.createPayload("windows", title, content, busType));
            }
            if (this.iosPbEnabled) {
                packet.setIosMessage((IosPayload) this.createPayload("ios", title, content, busType));
            }
        }
        if (packet.getAndroidMessage() != null || packet.getIosMessage() != null || packet.getWebMessage() != null
                || packet.getPcMessage() != null) {
            packets.add(packet);
            pushService.sendMultiple(packets, null);
            log.info("AbnormalTradePushNoticeServiceImpl.pushWebMessage complete，the content is : {}", content);
        } else {
            log.info(
                    "Not message to push,AbnormalTradePushNoticeServiceImpl.pushWebMessage complete，the content is : {}",
                    content);
        }
    }

    private AbstractNotification createPayload(String platform, String title, String content, String busType) {
        switch (platform) {
            case "web":
                WebPayload webMessage = new WebPayload();
                webMessage.setTitle(title);
                webMessage.setContent(content);
                webMessage.setBusType(busType);
                return webMessage;
            case "android":
                AndroidPayload androidPayload = new AndroidPayload();
                androidPayload.setTitle(title);
                androidPayload.setContent(content);
                androidPayload.setBusType(busType);
                return androidPayload;
            case "windows":
                PcPayload pcPayload = new PcPayload();
                pcPayload.setTitle(title);
                pcPayload.setContent(content);
                pcPayload.setBusType(busType);
                return pcPayload;
            case "ios":
                IosPayload iosPayload = new IosPayload();
                iosPayload.setTitle(title);
                iosPayload.setContent(content);
                iosPayload.setBusType(busType);
                return iosPayload;
        }
        return null;
    }


    @Override
    public void test() {
        String content = "QKCBTC,BTC,1543311780000,PriceBreakthroughs,,#{pb.tip1},null,3;";
        log.info("AbnormalTradePushNoticeServiceImpl.pushWebMessage ......，the content is : {}", content);
        List<MultipleNotificationPacket> packets = Lists.newArrayList();
        MultipleNotificationPacket packet = new MultipleNotificationPacket();
        WebPayload webMessage = new WebPayload();
        webMessage.setTitle("test");
        webMessage.setContent(content);
        webMessage.setBusType("AP");
        packet.setWebMessage(webMessage);
        packets.add(packet);
        pushService.sendMultiple(packets, null);
        log.info("AbnormalTradePushNoticeServiceImpl.pushWebMessage complete，the content is : {}", content);
    }



}
